// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:14:51
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Element\CBEAMParser.cpp
 */

#include "Import/BDFImport/BulkData/Element/CBEAMParser.h"

#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CBEAMData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CBEAM);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/CBEAM/CBEAM.md>
*/

// 以下需要修改
enum CBEAMKey : std::uint8_t
{
    CBEAMKey_EID  = 2,
    CBEAMKey_PID  = 3,
    CBEAMKey_GA   = 4,
    CBEAMKey_GB   = 5,
    CBEAMKey_X1   = 6,
    CBEAMKey_X2   = 7,
    CBEAMKey_X3   = 8,
    CBEAMKey_OFFT = 9,   // Offset vector interpretation flag. See Remark  9. (Character or blank)
    CBEAMKey_PA   = 12,  // Pin flags for beam ends A and B, respectively; used to remove connections between
                         // the grid point and selected degrees-of-freedom of the beam. The degrees-of-
                         // freedom are defined in the element’s coordinate system and the pin flags are
                         // applied at the offset ends of the beam (see  Figure 9-15 ). The beam must have
                         // stiffness associated with the PA and PB degrees-of-freedom to be released by the
                         // pin flags. For example, if PA = 4, the PBEAM entry must have a nonzero value
                         // for J, the torsional stiffness. (Up to five of the unique Integers 1 through 6 with
                         // no embedded blanks.) Pin flags are not allowed for beam p-elements. Pin flags
                         // combined with offsets are not allowed for SOL 600. Pin flags are not presently
                         // allowed in SOL 700. Also, Pig flags should not be used in nonlinear analysis when
                         // there is large displacement.
    CBEAMKey_PB  = 13,
    CBEAMKey_W1A = 14,   // Components of offset vectors from the grid points to the end points of the axis of
                         // the shear center. See Remarks  7. ,  8. and  9. (Real; Default = 0.0)
    CBEAMKey_W2A = 15,
    CBEAMKey_W3A = 16,
    CBEAMKey_W1B = 17,
    CBEAMKey_W2B = 18,
    CBEAMKey_W3B = 19,
    CBEAMKey_SA  = 22,  // Scalar or grid point identification numbers for the ends A and B, respectively. The
                        // degrees-of-freedom at these points are the warping variables  . SA and SB
                        // cannot be specified for beam p-elements. (Integers  0 or blank)
    CBEAMKey_SB = 23,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseCBEAM (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CBEAMData tempCBEAM;  // 创建一个临时的CBEAM数据对象

    // TODO(bug 001) m_dominId的正确填写
    tempCBEAM.m_domainId = 1;                // 暂时将m_domainId设为1，待修复

    int  curIDKey = 1;                       // 当前词条的索引，初始值为1
    bool isVector = false;                   // 用于判断X1、X2、X3是作为向量还是网格点编号

    for (auto curStrKey : dividedEntryInfo)  // 遍历分割后的词条信息
    {
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)                         // 如果当前词条为空，则跳过，处理下一个词条
        {
            curIDKey++;
            continue;
        }

        bool const isFloat = covertToScientificNotation (curStrKey);  // 检查当前词条是否为浮点数

        switch (curIDKey)                                             // 根据当前的词条索引，解析对应的数据
        {
        case CBEAMKey_EID:
            tempCBEAM.m_id = convertToI64 (curStrKey);                // 解析EID并存储到tempCBEAM的m_id
            break;
        case CBEAMKey_PID:
            tempCBEAM.m_pId = convertToI64 (curStrKey);               // 解析PID并存储到tempCBEAM的m_pId
            break;
        case CBEAMKey_GA:
            tempCBEAM.m_g[0] = convertToI64 (curStrKey);              // 解析GA网格点编号并存储到tempCBEAM的m_g[0]
            break;
        case CBEAMKey_GB:
            tempCBEAM.m_g[1] = convertToI64 (curStrKey);              // 解析GB网格点编号并存储到tempCBEAM的m_g[1]
            break;
        case CBEAMKey_X1:
            if (!isFloat)                                             // 如果X1不是浮点数，则视为网格点编号
            {
                tempCBEAM.m_go = convertToI64 (curStrKey);            // 存储网格点编号到tempCBEAM的m_go
            }
            else                                                      // 如果X1是浮点数，则视为向量分量
            {
                tempCBEAM.m_x1 = convertToReal (curStrKey);           // 存储X1向量分量到tempCBEAM的m_x1
                isVector       = true;                                // 设置isVector为true，表示后续X2、X3也是向量分量
            }
            break;
        case CBEAMKey_X2:
            if (isVector)                                    // 如果X1为向量，则处理X2向量分量
            {
                tempCBEAM.m_x2 = convertToReal (curStrKey);  // 存储X2向量分量到tempCBEAM的m_x2
            }
            break;
        case CBEAMKey_X3:
            if (isVector)                                    // 如果X1为向量，则处理X3向量分量
            {
                tempCBEAM.m_x3 = convertToReal (curStrKey);  // 存储X3向量分量到tempCBEAM的m_x3
            }
            break;
        case CBEAMKey_OFFT:
            // 去除首尾空格
            boost::trim (curStrKey);                                          // 去除当前词条字符串的首尾空格
            tempCBEAM.m_flag = SG::DataStructure::FEM::calcFlag (curStrKey);  // 计算OFFT标志并存储到tempCBEAM的m_flag
            break;
        case CBEAMKey_PA:
            tempCBEAM.m_pa = convertToI64 (curStrKey);                        // 解析并存储PA销钉标志
            break;
        case CBEAMKey_PB:
            tempCBEAM.m_pb = convertToI64 (curStrKey);                        // 解析并存储PB销钉标志
            break;
        case CBEAMKey_W1A:
            tempCBEAM.m_w1a = convertToReal (curStrKey);                      // 解析并存储W1A偏移分量
            break;
        case CBEAMKey_W2A:
            tempCBEAM.m_w2a = convertToReal (curStrKey);                      // 解析并存储W2A偏移分量
            break;
        case CBEAMKey_W3A:
            tempCBEAM.m_w3a = convertToReal (curStrKey);                      // 解析并存储W3A偏移分量
            break;
        case CBEAMKey_W1B:
            tempCBEAM.m_w1b = convertToReal (curStrKey);                      // 解析并存储W1B偏移分量
            break;
        case CBEAMKey_W2B:
            tempCBEAM.m_w2b = convertToReal (curStrKey);                      // 解析并存储W2B偏移分量
            break;
        case CBEAMKey_W3B:
            tempCBEAM.m_w3b = convertToReal (curStrKey);                      // 解析并存储W3B偏移分量
            break;
        case CBEAMKey_SA:
            tempCBEAM.m_sa = convertToI64 (curStrKey);                        // 解析并存储SA标志
            break;
        case CBEAMKey_SB:
            tempCBEAM.m_sb = convertToI64 (curStrKey);                        // 解析并存储SB标志
            break;
        default:
            break;
        }
        curIDKey++;  // 处理完当前词条后，索引加1
    }

    // 将解析后的CBEAM数据添加到数据库中
    return dbServiceFactory->get<IElementService> ()->Add (tempCBEAM);
}